home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993 October: Windmill on DISC / ADC Developer CD (1993-10) (''Windmill On DISC'')_iso / Dev.CD Oct 93.iso / System Software / U.S. System Software / System 7 Pro™ Beta 11 / Development Tools / Sample Code / Standard Mail / CollaboDraw (w⁄DigiSign) / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  11.5 KB  |  591 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * Simple Sample AOCE Application Framework
  4.  *
  5.  * ©1991-1993 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * utils.c -- utility routines
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        04/21/93        1.0b2        update to b2
  14.  * SJF        03/01/93        1.0b1        added digital signatures
  15.  * SJF        02/09/93        1.0b1        update to b1
  16.  * SJF        10/13/92        1.0d4        update to a11
  17.  * SJF        09/09/92        1.0d3        update to a9
  18.  * SJF        05/07/92        1.0d2        update to a6
  19.  * SJF        11/06/91        1.0d1        initial coding
  20.  *
  21.  */
  22.  
  23. #ifndef __GESTALTEQU__
  24. #include <GestaltEqu.h>
  25. #endif
  26.  
  27. #ifndef __TRAPS__
  28. #include <Traps.h>
  29. #endif
  30.  
  31. #ifndef __NOTIFICATION__
  32. #include <Notification.h>
  33. #endif
  34.  
  35. #ifndef __TOOLUTILS__
  36. #include <ToolUtils.h>
  37. #endif
  38.  
  39. #ifndef __FILES__
  40. #include <Files.h>
  41. #endif
  42.  
  43. #ifndef __PACKAGES__
  44. #include <Packages.h>
  45. #endif
  46.  
  47. #ifndef __EVENTS__
  48. #include <Events.h>
  49. #endif
  50.  
  51. #ifndef __RESOURCES__
  52. #include <Resources.h>
  53. #endif
  54.  
  55. #ifndef __FOLDERS__
  56. #include <Folders.h>
  57. #endif
  58.  
  59. #ifndef __PALETTES__
  60. #include <Palettes.h>
  61. #endif
  62.  
  63. #ifndef __OCESTANDARDMAIL__
  64. #incude <OCEStandardMail.h>
  65. #endif
  66.  
  67. #include "const.h"
  68. #include "mytypes.h"
  69. #include "globals.h"
  70. #include "trapavailable.h"
  71. #include "main.h"
  72. #include "mymenus.h"
  73. #include "strconst.h"
  74. #include "windowstuff.h"
  75.  
  76. #include "utils.h"
  77.  
  78. Boolean HasColorQD(void)
  79. {
  80.     SysEnvRec theEnv;
  81.     
  82.     if (SysEnvirons(1,&theEnv) != noErr)
  83.         return false;
  84.  
  85.     return (theEnv.hasColorQD);
  86. }
  87.  
  88.  
  89. void DoError(OSErr err)
  90. {
  91.     Str255 errStr;
  92.     Str255 errNumStr;
  93.     Handle resHandle;
  94.     
  95.     if (err==noErr)
  96.         return;
  97.     
  98.     if (resHandle=GetResource('Estr',err)) {
  99.         HLock(resHandle);
  100.         pstrcpy(errStr,*resHandle);
  101.         HUnlock(resHandle);
  102.         ReleaseResource(resHandle);
  103.     }
  104.     else {
  105.         NumToString(err,errNumStr);
  106.         pstrcpy(errStr,"\pAn error has occurred: ");
  107.         pstrcat(errStr,errNumStr);
  108.     }
  109.     
  110.     if (gInBackground)
  111.         Notify(errStr);
  112.     else {
  113.         ParamText(errStr,nil,nil,nil);
  114.         StopAlert(kErrorBoxID,nil);
  115.     }    
  116. }
  117.  
  118.  
  119. void Notify(StringPtr string)
  120. {
  121.     NMRecPtr nm;
  122.     StringPtr strPtr;
  123.     
  124.     nm = (NMRecPtr)NewPtr(sizeof(NMRec));
  125.     if (MemError()!=noErr)
  126.         return;
  127.     strPtr = (StringPtr)NewPtr(string[0]);
  128.     if (MemError()!=noErr)
  129.         return;
  130.     BlockMove(string,strPtr,string[0]+1);
  131.     
  132.     nm->qType = nmType;
  133.     nm->nmMark = 0;
  134.     nm->nmIcon = nil;
  135.     nm->nmSound = nil;
  136.     nm->nmStr = strPtr;
  137.     nm->nmResp = nil;
  138.     NMInstall(nm);
  139. }
  140.  
  141.  
  142. void pstrcpy(void *dest,void *src)
  143. {
  144.     unsigned char srcLen = ((unsigned char *)src)[0];
  145.     
  146.     BlockMove(src,dest,srcLen+1);
  147. }
  148.  
  149.  
  150. void pstrcat(void *original,void *catStr)
  151. {
  152.     short length;
  153.     unsigned char originalLen = ((unsigned char *)original)[0];
  154.     unsigned char catStrLen = ((unsigned char *)catStr)[0];
  155.     
  156.     length = (short) originalLen;
  157.     length += (short) catStrLen;
  158.     
  159.     if (length > 255) {
  160.         DebugStr("\pstring catenation overflow");
  161.         ExitProc();
  162.     }
  163.     
  164.     BlockMove((char *)catStr+1,(char *)original+originalLen+1,catStrLen);
  165.     ((unsigned char *)original)[0] = (unsigned char) length;
  166. }
  167.  
  168.  
  169. void GetResString(StringPtr storage,short rezID,StringPtr backupString)
  170. {
  171.     StringHandle rezString;
  172.     
  173.     rezString = GetString(rezID);
  174.     if (rezString) {
  175.         pstrcpy(storage,*rezString);
  176.         ReleaseResource((Handle)rezString);
  177.     }
  178.     else
  179.         pstrcpy(storage,backupString);
  180. }
  181.  
  182.  
  183. void *NewPtrChk(Size ptrSize)
  184. {
  185.     Ptr thePtr;
  186.  
  187.     thePtr = NewPtr(ptrSize);
  188.     if (MemError()!=noErr)
  189.         DoError(MemError());
  190. #if kDEBUG
  191.     {
  192.         long *longPtr = (long *)thePtr;
  193.         *longPtr = kBetterBusErr;
  194.     }
  195. #endif
  196.     return thePtr;
  197. }
  198.  
  199.  
  200. void *NewHandleChk(Size hndlSize)
  201. {
  202.     Handle theHndl;
  203.     
  204.     theHndl = NewHandle(hndlSize);
  205.     if (MemError()!=noErr)
  206.         DoError(MemError());
  207. #if kDEBUG
  208.     {
  209.         long **longHndl = (long **)theHndl;
  210.         **longHndl = kBetterBusErr;
  211.     }
  212. #endif
  213.     return theHndl;
  214. }
  215.  
  216.  
  217. void DisposPtrChk(void *thePtr)
  218. {
  219. #if kDEBUG
  220.     {
  221.         long *longPtr = (long *)thePtr;
  222.         *longPtr = kBetterBusErr;
  223.     }
  224. #endif
  225.  
  226.     DisposPtr(thePtr);
  227.     if (MemError()!=noErr)
  228.         DoError(MemError());
  229. }
  230.  
  231.  
  232. void DisposHandleChk(void *theHndl)
  233. {
  234. #if kDEBUG
  235.     {
  236.         long **longHndl = (long **)theHndl;
  237.         **longHndl = kBetterBusErr;
  238.     }
  239. #endif
  240.  
  241.     DisposHandle(theHndl);
  242.     if (MemError()!=noErr)
  243.         DoError(MemError());
  244. }
  245.  
  246.  
  247. void ExitProc(void)
  248. {
  249.     ExitToShell();
  250. }
  251.  
  252.  
  253. void DisableAllMenus(void)
  254. {
  255.     MenuHandle theMenu;
  256.     short menuIndex;
  257.     
  258.     for (menuIndex=kAppleMenu+1; menuIndex<kAppleMenu+kNumMenus; menuIndex++) {
  259.         theMenu = GetMHandle(menuIndex);
  260.         DisableItem(theMenu,0);
  261.     }
  262. }
  263.  
  264.  
  265. void EnableAllMenuItems(MenuHandle theMenu)
  266. {
  267.     short itemIndex,numItems;
  268.     
  269.     numItems = CountMItems(theMenu);
  270.     for (itemIndex=1; itemIndex<=numItems; itemIndex++)
  271.         EnableItem(theMenu,itemIndex);
  272.     EnableItem(theMenu,0);
  273. }
  274.  
  275.  
  276.  
  277. void SetDefaultMenus(void)
  278. {
  279.     MenuHandle theMenu;
  280.         
  281.     DisableAllMenus();
  282.     theMenu = GetMHandle(kFileMenu);
  283.     EnableAllMenuItems(theMenu);
  284.     DisableItem(theMenu,kCloseItem);
  285.     DisableItem(theMenu,kSaveItem);
  286.     DisableItem(theMenu,kSaveAsItem);
  287.     DisableItem(theMenu,kPageSetupItem);
  288.     DisableItem(theMenu,kPrintItem);
  289.     
  290.     theMenu = GetMHandle(kEditMenu);
  291.     EnableAllMenuItems(theMenu);
  292.     DisableItem(theMenu,kUndoItem);
  293.     DisableItem(theMenu,kCutItem);
  294.     DisableItem(theMenu,kCopyItem);
  295.     DisableItem(theMenu,kPasteItem);
  296.     DisableItem(theMenu,kClearItem);
  297.     DisableItem(theMenu,kSelectAllItem);
  298.     
  299.     theMenu = GetMHandle(kMailMenu);
  300.     EnableAllMenuItems(theMenu);
  301.     DisableItem(theMenu,kAddRemMailItem);
  302.     DisableItem(theMenu,kSendItem);
  303.     DisableItem(theMenu,kReplyItem);
  304.     DisableItem(theMenu,kReplyToAllItem);
  305.     DisableItem(theMenu,kForwardItem);
  306.     DisableItem(theMenu,kTagLetterItem);
  307.  
  308.     theMenu = GetMHandle(kSignMenu);
  309.     DisableItem(theMenu,0);
  310. }
  311.  
  312.  
  313. void SetUndoCommand(WindowPtr window,WInfoPtr infoPtr,const ShapeList *command)
  314. {
  315.     gCanUndo = true;
  316.     gHasUndo = false;
  317.     gUndoCommand.window = window;
  318.     gUndoCommand.theShape = *command;
  319.     gUndoCommand.windowRefCount = infoPtr->refCount;
  320. }
  321.  
  322.  
  323. void ClearAppUndo(void)
  324. {
  325.     gCanUndo = false;
  326.     gHasUndo = false;
  327. }
  328.  
  329.  
  330. Boolean SetupAppUndo(void)
  331. {
  332.     if (gCanUndo)
  333.         return SetUndoString(gUndoCommand.theShape.shapeType,gHasUndo);
  334.     else
  335.         return SetUndoString(0,false);
  336. }
  337.  
  338.  
  339. Boolean SetUndoString(short commandID,Boolean redoFlag)
  340. {
  341.     Str255 undoString;
  342.     MenuHandle theMenu;
  343.     WInfoHndl infoHndl;
  344.     
  345.     if (redoFlag) {
  346.         switch (commandID) {
  347.             case kLineShape:
  348.                 GetResString(undoString,kRedoLineID,kRedoLine);
  349.                 break;
  350.             case kRectShape:
  351.                 GetResString(undoString,kRedoRectID,kRedoRect);
  352.                 break;
  353.             case kRoundRectShape:
  354.                 GetResString(undoString,kRedoRoundRectID,kRedoRoundRect);
  355.                 break;
  356.             case kOvalShape:
  357.                 GetResString(undoString,kRedoOvalID,kRedoOval);
  358.                 break;
  359.         }
  360.     }
  361.     else {
  362.         switch (commandID) {
  363.             case 0:
  364.                 GetResString(undoString,kUndoStringID,kUndoString);
  365.                 break;
  366.             case kLineShape:
  367.                 GetResString(undoString,kUndoLineID,kUndoLine);
  368.                 break;
  369.             case kRectShape:
  370.                 GetResString(undoString,kUndoRectID,kUndoRect);
  371.                 break;
  372.             case kRoundRectShape:
  373.                 GetResString(undoString,kUndoRoundRectID,kUndoRoundRect);
  374.                 break;
  375.             case kOvalShape:
  376.                 GetResString(undoString,kUndoOvalID,kUndoOval);
  377.                 break;
  378.         }
  379.     }
  380.     
  381.     theMenu = GetMHandle(kEditMenu);
  382.     SetItem(theMenu,kUndoItem,undoString);
  383.  
  384.     if (FrontWindow())
  385.         infoHndl = GetWindowInfo(FrontWindow());
  386.     
  387.     if (gCanUndo && FrontWindow() && ((**infoHndl).refCount==gUndoCommand.windowRefCount) && commandID!=0) {
  388.         EnableItem(theMenu,kUndoItem);
  389.         return true;
  390.     }
  391.     else {
  392.         DisableItem(theMenu,kUndoItem);
  393.         return false;
  394.     }
  395. }
  396.  
  397.  
  398. void ReadPrefs(void)
  399. {
  400.     FSSpec prefsSpec;
  401.     short refNum;
  402.     long count;
  403.     OSErr err;
  404.     
  405.     refNum = 0;
  406.     
  407.     GetPrefsFolder(&prefsSpec);
  408.     err = FSpOpenDF(&prefsSpec,fsRdPerm,&refNum);
  409.     if (err!=noErr) {
  410.         err = FSpCreate(&prefsSpec,kAppCreator,kPrefsType,smRoman);
  411.         if (err==noErr)
  412.             err = FSpOpenDF(&prefsSpec,fsRdPerm,&refNum);
  413.     }
  414.     
  415.     if (err==noErr) {
  416.         count = sizeof(MyPreferences);
  417.         err = FSRead(refNum,&count,&gPreferences);
  418.         if (err==noErr && gPreferences.version!=kPrefsVersion)
  419.             err = -1;
  420.     }
  421.     
  422.     if (err!=noErr) {
  423.     
  424.         // set default preferences
  425.         
  426.         gPreferences.version = kPrefsVersion;
  427.         gPreferences.sendOptions.signWhenSent = false;
  428.         gPreferences.sendOptions.priority = kIPMNormalPriority;
  429.         gPreferences.closeOptions.moveToTrash = false;
  430.         gPreferences.closeOptions.addTag = false;
  431.         gPreferences.closeOptions.tag.dataLength = 0;
  432.         gPreferences.sendFormat.whichFormats = kSMPNativeMask | kSMPStandardInterchangeMask;
  433.         gPreferences.sendFormat.whichNativeFormat = 0;
  434.         gPreferences.closeOnSend = false;
  435.         gPreferences.closeOptionsDialog = true;
  436.         gPreferences.expandOnCreate = true;
  437.         gPreferences.expandOnOpen = false;
  438.     }
  439.     
  440.     if (refNum!=0)
  441.         FSClose(refNum);
  442. }
  443.  
  444.  
  445. void WritePrefs(void)
  446. {
  447.     FSSpec prefsSpec;
  448.     short refNum;
  449.     long count;
  450.     OSErr err;
  451.     
  452.     GetPrefsFolder(&prefsSpec);
  453.     
  454.     refNum = 0;
  455.     
  456.     err = FSpOpenDF(&prefsSpec,fsRdWrPerm,&refNum);
  457.     if (err!=noErr) {
  458.         err = FSpCreate(&prefsSpec,kAppCreator,kPrefsType,smRoman);
  459.         if (err==noErr)
  460.             err = FSpOpenDF(&prefsSpec,fsRdWrPerm,&refNum);
  461.     }
  462.  
  463.     if (err==noErr) {
  464.         count  = sizeof(MyPreferences);
  465.         err = FSWrite(refNum,&count,&gPreferences);
  466.     }
  467.     
  468.     if (refNum!=0)
  469.         FSClose(refNum);
  470. }
  471.  
  472.  
  473. void GetPrefsFolder(FSSpec *fSpec)
  474. {
  475.     long gestResponse;
  476.     SysEnvRec sysEnv;
  477.     WDPBRec pBlock;
  478.     Str255 fName;
  479.     OSErr err;
  480.     
  481.     err = -1;
  482.     
  483.     if (TrapAvailable(_Gestalt) && 
  484.         (Gestalt(gestaltFindFolderAttr,&gestResponse)==noErr) &&
  485.         (gestResponse && (1<<gestaltFindFolderPresent))) {
  486.             err = FindFolder(kOnSystemDisk,kPreferencesFolderType,false,&fSpec->vRefNum,&fSpec->parID);
  487.     }
  488.  
  489.     if (err!=noErr) {
  490.         SysEnvirons(1,&sysEnv);
  491.         fName[0] = 0;
  492.         pBlock.ioVRefNum = sysEnv.sysVRefNum;
  493.         pBlock.ioNamePtr = fName;
  494.         pBlock.ioWDIndex = 0;
  495.         pBlock.ioWDProcID = 0;
  496.         pBlock.ioWDVRefNum = 0;
  497.         err = PBGetWDInfo(&pBlock,false);
  498.         if (err!=noErr) {
  499.             fSpec->vRefNum = -1;
  500.             fSpec->parID = fsRtDirID;
  501.         }
  502.     }
  503.     
  504.     GetResString(fSpec->name,kPrefsFileID,kPrefsFile);
  505. }
  506.  
  507.  
  508. #define    kButtonFrameInset    -4
  509. #define    kButtonFrameSize    3
  510. #define    kCntrActivate        0
  511. #define    kColorPortMask        0xc000
  512. #define    kActivateControl    0
  513.  
  514. void MyDrawDefaultButtonOutline(DialogPtr theDialog,short theItem)
  515. {
  516.     short itemType;
  517.     Rect itemRect;
  518.     ControlHandle itemHandle;
  519.     PenState curPen;
  520.     short buttonOval;
  521.     RGBColor fgSaveColor,bgColor,newFGColor;
  522.     Boolean newGray;
  523.     WindowPtr oldPort;
  524.     Boolean isColor;
  525.     GDHandle targetDevice;
  526.     
  527.     // get the default button and draw a bold border around it
  528.     
  529.     GetDItem(theDialog,theItem,&itemType,(Handle *)&itemHandle,&itemRect);
  530.     GetPort(&oldPort);
  531.     SetPort((**itemHandle).contrlOwner);
  532.     GetPenState(&curPen);
  533.  
  534.     PenNormal();
  535.     InsetRect(&itemRect,kButtonFrameInset,kButtonFrameInset);
  536.     FrameRoundRect(&itemRect,16,16);
  537.  
  538.     buttonOval = ((itemRect.bottom-itemRect.top)/2) + 2;
  539.     if (((CGrafPtr)((**itemHandle).contrlOwner))->portVersion & kColorPortMask)
  540.         isColor = true;
  541.     else
  542.         isColor = false;
  543.     
  544.     if ((**itemHandle).contrlHilite != kActivateControl) {    // control is dimmed, so draw gray
  545.         newGray = false;
  546.         if (isColor) {
  547.             GetBackColor(&bgColor);
  548.             GetForeColor(&fgSaveColor);
  549.             newFGColor = fgSaveColor;
  550.             targetDevice = MyGetDeviceFromRect(&(**itemHandle).contrlRect);
  551.             newGray = GetGray(targetDevice,&bgColor,&newFGColor);
  552.         }
  553.         if (newGray)
  554.             RGBForeColor(&newFGColor);
  555.         else
  556.             PenPat(qd.gray);
  557.         PenSize(kButtonFrameSize,kButtonFrameSize);
  558.         FrameRoundRect(&itemRect,buttonOval,buttonOval);
  559.         if (isColor)
  560.             RGBForeColor(&fgSaveColor);
  561.     }
  562.     else {
  563.         PenPat(qd.black);
  564.         PenSize(kButtonFrameSize,kButtonFrameSize);
  565.         FrameRoundRect(&itemRect,buttonOval,buttonOval);
  566.     }
  567.     
  568.     SetPenState(&curPen);
  569.     SetPort(oldPort);
  570. }
  571.  
  572.  
  573. GDHandle MyGetDeviceFromRect(Rect *localRect)
  574. {
  575.     GDHandle device;
  576.     Point wCenter;
  577.     
  578.     wCenter.v = localRect->bottom-localRect->top;
  579.     wCenter.h = localRect->right-localRect->left;
  580.     LocalToGlobal(&wCenter);
  581.     
  582.     device = GetDeviceList();
  583.     while (device) {
  584.         if (PtInRect(wCenter,&(**device).gdRect))
  585.             return device;
  586.         device = GetNextDevice(device);
  587.     }
  588.     
  589.     return GetMainDevice();
  590. }
  591.